package com.marvels.product.param.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.marvels.common.constants.MarvelsConstant;
import com.marvels.common.dto.UserDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.CacheUtils;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.product.param.dao.McptRateMapper;
import com.marvels.product.param.dto.McptRateDTO;
import com.marvels.product.param.service.McptRateService;
@Service
public class McptRateServiceImpl implements McptRateService {
	
	@Autowired
	private McptRateMapper rateMapper;

	@Override
	public int deleteByPrimaryKey(Long rateId) throws MarvelsBusinessException{
		return rateMapper.deleteByPrimaryKey(rateId);
	}

	@Override
	public int insert(McptRateDTO record) throws MarvelsBusinessException{
		return rateMapper.insert(record);
	}

	@Override
	public McptRateDTO selectByPrimaryKey(Long rateId) throws MarvelsBusinessException{
		return rateMapper.selectByPrimaryKey(rateId);
	}

	@Override
	public int updateByPrimaryKey(McptRateDTO record) throws MarvelsBusinessException{
		return rateMapper.updateByPrimaryKey(record);
	}

	@Override
	public PageInfo<McptRateDTO> getPageInfoByParam(Map<String, Object> param) throws MarvelsBusinessException{
		
		Integer currentPage = param.get("currentPage")==null?1:(Integer)param.get("currentPage");
		
		Integer pageSize = param.get("pageSize")==null?20:(Integer)param.get("pageSize");
		
		PageHelper.startPage(currentPage, pageSize);
		
		List<McptRateDTO>  rateList = rateMapper.getListByParam(param);
		
		PageInfo<McptRateDTO> pageInfo = new PageInfo<McptRateDTO>(rateList);
	    
		return pageInfo;
	}

	public int updateStatus(McptRateDTO dto) throws MarvelsBusinessException{
		Integer count = 0;
		try {
			String userName = MarvelsConstant.SYSTEM_USER;
			 UserDTO user = CacheUtils.getCacheUser();
			 if(user!=null){
				 userName = user.getUserName();
			 }
			 dto.setUpdatedBy(userName);
			 count = rateMapper.updateStatus(dto);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>利率审核异常",e);
			throw new MarvelsBusinessException(e);
		}
		return count;
	}
	


	
	
}
